package com.benyi.util;
 
import java.util.Scanner;

public class CalculatorDs {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter an expression: "); 
        String input = scanner.nextLine(); 
        while(input.length()>0) {
        	ExpressionParser parser = new ExpressionParser(input);
        	try {
                double result = parser.parse();
                System.out.println("Result: " + result);
            } catch (Exception e) {
                System.out.println("Error: " + e.getMessage());
            }
        	System.out.print("Enter an expression: "); 
        	input = scanner.nextLine();
        }  
        scanner.close();
    }
}

class ExpressionParser {
    private String str;
    private int index;
    private char currentChar;

    public ExpressionParser(String str) {
        this.str = str.replaceAll("\\s+", ""); // 移除所有空格
        this.index = 0;
        this.currentChar = this.str.length() > 0 ? this.str.charAt(0) : '\0';
    }

    private void nextChar() {
        index++;
        if (index < str.length()) {
            currentChar = str.charAt(index);
        } else {
            currentChar = '\0';
        }
    }

    public double parse() {
        return parseExpression();
    }

    private double parseExpression() {
        double result = parseTerm();
        while (currentChar == '+' || currentChar == '-') {
            char op = currentChar;
            nextChar();
            double term = parseTerm();
            if (op == '+') {
                result += term;
            } else {
                result -= term;
            }
        }
        return result;
    }

    private double parseTerm() {
        double result = parseFactor();
        while (currentChar == '*' || currentChar == '/') {
            char op = currentChar;
            nextChar();
            double factor = parseFactor();
            if (op == '*') {
                result *= factor;
            } else {
                if (factor == 0) {
                    throw new ArithmeticException("Division by zero");
                }
                result /= factor;
            }
        }
        return result;
    }

    private double parseFactor() {
        if (currentChar == '(') {
            nextChar();
            double result = parseExpression();
            if (currentChar != ')') {
                throw new RuntimeException("Missing closing parenthesis");
            }
            nextChar();
            return result;
        } else if (currentChar == '-') {
            nextChar();
            return -parseFactor();
        } else if (Character.isDigit(currentChar) || currentChar == '.') {
            return parseNumber();
        } else {
            throw new RuntimeException("Unexpected character: " + currentChar);
        }
    }

    private double parseNumber() {
        StringBuilder sb = new StringBuilder();
        while (Character.isDigit(currentChar) || currentChar == '.') {
            sb.append(currentChar);
            nextChar();
        }
        try {
            return Double.parseDouble(sb.toString());
        } catch (NumberFormatException e) {
            throw new RuntimeException("Invalid number format: " + sb.toString());
        }
    }
}